/*
 * ThreadHeader.java
 *
 * Created on 2005/01/21 +0900
 *
 * $Revision: 38 $
 * $Date: 2008-10-11 08:48:45 +0000 (Sat, 11 Oct 2008) $
 */

package org.gikolet.bbs.data;

import java.util.Date;

import org.gikolet.framework.util.Assert;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 38 $ $Date: 2008-10-11 08:48:45 +0000 (Sat, 11 Oct 2008) $
 */
public class ThreadHeader {
    private Board board;
    private String key;
    private String subject;

    private Date lastModified;
    private int resCount;

    private int boardHashCodeCache;
    private int keyHashCodeCache;

    private int hashCodeCache;

    public ThreadHeader(Board board, String key, String subject) {
	this(board, key, subject, 0, new Date());
    }

    public ThreadHeader(Board board, String key, String subject, int resCount) {
	this(board, key, subject, resCount, new Date());
    }

    public ThreadHeader(Board board, String key, String subject, int resCount,
	    Date lastModified) {
	Assert.isNotNull(board);
	Assert.isNotNullAndEmpty(key);
	Assert.isNotNullAndEmpty(subject);
	Assert.isNotTrue(resCount < 0);
	Assert.isNotNull(lastModified);

	this.board = board;
	this.key = key;
	this.subject = subject;
	this.resCount = resCount;
	this.lastModified = lastModified;

	this.boardHashCodeCache = 0;
	this.keyHashCodeCache = 0;
	this.hashCodeCache = (this.boardHashCodeCache + ":" + this.keyHashCodeCache)
		.hashCode();
    }

    public Board getBoard() {
	return this.board;
    }

    public void setBoard(Board board) {
	Assert.isNotNull(board);

	this.board = board;
    }

    public String getKey() {
	return this.key;
    }

    public void setKey(String key) {
	Assert.isNotNullAndEmpty(key);

	this.key = key;
    }

    public String getSubject() {
	return this.subject;
    }

    public void setSubject(String subject) {
	Assert.isNotNullAndEmpty(subject);

	this.subject = subject;
    }

    public int getResCount() {
	return this.resCount;
    }

    public void setResCount(int resCount) {
	Assert.isNotTrue(resCount < 0);

	this.resCount = resCount;
    }

    public Date getLastModified() {
	return this.lastModified;
    }

    public void setLastModified(Date lastModified) {
	Assert.isNotNull(lastModified);

	this.lastModified = lastModified;
    }

    public int hashCode() {
	int boardHashCode = this.board.hashCode();
	int keyHashCode = this.key.hashCode();

	if (boardHashCode != this.boardHashCodeCache
		|| keyHashCode != this.keyHashCodeCache) {
	    this.hashCodeCache = (boardHashCode + ":" + keyHashCode).hashCode();
	}
	return this.hashCodeCache;
    }

    public boolean equals(Object obj) {
	if (obj instanceof ThreadHeader) {
	    return equals((ThreadHeader) obj);
	}
	return false;
    }

    public boolean equals(ThreadHeader threadHeader) {
	if (threadHeader == null) {
	    return false;
	}
	return (this.board.equals(threadHeader.board) && this.key
		.equals(threadHeader.key));
    }

    public String toString() {
	return this.board.toString() + ", threadKey=" + this.key + ", subject="
		+ this.subject + ", resCount=" + this.resCount + ", lastModified="
		+ this.lastModified;
    }
}
